package com.own.business.system.user.controller.user;

import com.own.business.system.user.business.service.SystemUserInfoService;
import com.own.business.system.user.business.service.SystemUserService;
import com.own.business.system.user.controller.user.entity.form.UpdateNameForm;
import com.own.business.system.user.controller.user.entity.form.UpdateUserAvatarForm;
import com.own.business.system.user.controller.user.entity.form.UpdateUserEmailForm;
import com.own.business.system.user.controller.user.entity.form.UpdateUserNameForm;
import com.own.business.system.user.controller.user.entity.form.UpdateUserPhoneForm;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * UserSystemUserInfoController
 *
 * @author chenxueli
 * @date 2023-08-09 15:49:45
 */
@Slf4j
@Tag(name = "系统用户基本信息" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + "user/system/user/info")
public class UserSystemUserInfoController extends BaseController {

    @Resource
    private SystemUserInfoService systemUserInfoService;

    @Resource
    private SystemUserService systemUserService;

    @LogOperationAop
    @Operation(summary = "修改昵称")
    @PutMapping("/update/name")
    public ResultModel<Boolean> updateName(
            @RequestBody UpdateNameForm form
    ) {
        var loginUser = SessionUserUtil.getLoginUser();
        systemUserInfoService.updateName(loginUser.userId(), form.getName());
        return R.success(true);
    }

    @LogOperationAop
    @Operation(summary = "注册天数")
    @GetMapping("/register/day")
    public ResultModel<Long> registerDay() {
        var loginUser = SessionUserUtil.getLoginUser();
        var days = systemUserService.registerDay(loginUser.userId());
        return R.success(days);
    }


    @LogOperationAop
    @Operation(summary = "修改用户名（登录用户的）")
    @PutMapping("/self/user/name")
    public ResultModel<Boolean> updateSelfUserName(
            @RequestBody UpdateUserNameForm form
    ) {
        form.check();
        var loginUser = SessionUserUtil.getLoginUser();
        systemUserInfoService.updateName(loginUser.userId(), form.getUserName());
        return R.success(true);
    }

    @LogOperationAop
    @Operation(summary = "修改头像（登录用户的）")
    @PutMapping("/self/avatar")
    public ResultModel<Boolean> updateSelfAvatar(
            @RequestBody UpdateUserAvatarForm form
    ) {
        form.check();
        var loginUser = SessionUserUtil.getLoginUser();
        systemUserInfoService.updateAvatar(loginUser.userId(), form.getAvatar());
        return R.success(true);
    }

    @LogOperationAop
    @Operation(summary = "修改手机号（登录用户的）")
    @PutMapping("/self/phone")
    public ResultModel<Boolean> updateSelfPhone(
            @RequestBody UpdateUserPhoneForm form
    ) {
        form.check();
        var loginUser = SessionUserUtil.getLoginUser();
        systemUserInfoService.updatePhone(loginUser.userId(), form.getPhone());
        return R.success(true);
    }

    @LogOperationAop
    @Operation(summary = "修改邮箱（登录用户的）")
    @PutMapping("/self/email")
    public ResultModel<Boolean> updateSelfEmail(
            @RequestBody UpdateUserEmailForm form
    ) {
        form.check();
        var loginUser = SessionUserUtil.getLoginUser();
        systemUserInfoService.updateEmail(loginUser.userId(), form.getEmail());
        return R.success(true);
    }

}
